package com.yhxd.modular.system.entity;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.yhxd.modular.institutions.entity.Dept;
import com.yhxd.utils.EncodeUtils;
import com.yhxd.utils.RedissonUtils;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.hibernate.annotations.GenericGenerator;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;

import javax.persistence.*;
import java.io.Serializable;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 用户
 * @author wsf
 */
@Data
@Entity(name = "sys_user")
@EntityListeners(value = AuditingEntityListener.class)
public class User implements Serializable {

    @ApiModelProperty(value = "id")
    @Id
    @GeneratedValue(generator = "idGenerator")
    @GenericGenerator(name = "idGenerator", strategy = "com.yhxd.utils.IdUtils")
    @Column(name = "id")
    private Long id;

    @ApiModelProperty(value = "用户名")
    @Column(name = "username")
    private String username;

    @ApiModelProperty(value = "密码")
    @Column(name = "password")
    private String password;

    @ApiModelProperty(value = "密码盐")
    @Column(name = "salt")
    private String salt;

    @ApiModelProperty(value = "真实姓名")
    @Column(name = "real_name")
    private String realName;

    @ApiModelProperty(value = "生日")
    @Temporal(TemporalType.DATE)
    @Column(name = "birthday")
    private Date birthday;

    @ApiModelProperty(value = "性别，0男，1女")
    @Column(name = "sex")
    private Integer sex;

    @ApiModelProperty(value = "注册时间")
    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "reg_time")
    private Date regTime;

    @ApiModelProperty(value = "注册ip")
    @Column(name = "reg_ip")
    private String regIp;

    @ApiModelProperty(value = "移动电话")
    @Column(name = "mobile")
    private String mobile;

    @ApiModelProperty(value = "办公电话")
    @Column(name = "phone")
    private String phone;

    @ApiModelProperty(value = "内部电话")
    @Column(name = "inner_phone")
    private String innerPhone;

    @ApiModelProperty(value = "参加工作时间")
    @Column(name = "work_date")
    private String workDate;

    @ApiModelProperty(value = "入职时间")
    @Temporal(TemporalType.DATE)
    @Column(name = "entry_date")
    private Date entryDate;

    @ApiModelProperty(value = "人员类别id")
    @Column(name = "personnel_type_id", insertable = false, updatable = false)
    private Long personnelTypeId;

    @ApiModelProperty(value = "人员类别")
    @ManyToOne(cascade = CascadeType.REFRESH, fetch = FetchType.LAZY)
    @JoinColumn(name = "personnel_type_id")
    @org.hibernate.annotations.ForeignKey(name = "none")
    private PersonnelType personnelType;

    @ApiModelProperty(value = "证件类型id")
    @Column(name = "cert_type_id", insertable = false, updatable = false)
    private Long certTypeId;

    @ApiModelProperty(value = "证件类型")
    @ManyToOne(cascade = CascadeType.REFRESH, fetch = FetchType.LAZY)
    @JoinColumn(name = "cert_type_id")
    @org.hibernate.annotations.ForeignKey(name = "none")
    private CertType certType;

    @ApiModelProperty(value = "证件号码")
    @Column(name = "cert_no")
    private String certNo;

    @ApiModelProperty(value = "是否领导，0否，1是")
    @Column(name = "leader")
    private Integer leader;

    @ApiModelProperty(value = "是否正职，0副职，1正职")
    @Column(name = "formal")
    private Integer formal;

    @ApiModelProperty(value = "机构id")
    @Column(name = "dep_id", insertable = false, updatable = false)
    private Long deptId;

    @ApiModelProperty(value = "机构")
    @OneToOne(cascade = CascadeType.REFRESH, fetch = FetchType.LAZY)
    @JoinColumn(name = "dep_id")
    @org.hibernate.annotations.ForeignKey(name = "none")
    private Dept dept;

    @ApiModelProperty(value = "最后登录时间")
    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "last_time")
    private Date lastTime;

    @ApiModelProperty(value = "最后登录ip")
    @Column(name = "last_ip")
    private String lastIp;

    @ApiModelProperty(value = "是否是超级管理员，0不是1是")
    @Column(name = "admin")
    private Integer admin = 0;

    @ApiModelProperty(value = "创建人")
    @Column(name = "create_name")
    private String createName;

    @ApiModelProperty(value = "创建时间")
    @Temporal(TemporalType.TIMESTAMP)
    @CreatedDate
    @Column(name = "create_time")
    private Date createTime;

    @ApiModelProperty(value = "修改人")
    @Column(name = "update_name")
    private String updateName;

    @ApiModelProperty(value = "修改时间")
    @Temporal(TemporalType.TIMESTAMP)
    @LastModifiedDate
    @Column(name = "update_time")
    private Date updateTime;

    @ApiModelProperty(value = "数据是否可用，0不可用，1可用")
    @Column(name = "enable")
    private Integer enable = 1;

    @ApiModelProperty(value = "版本号")
    @Column(name = "version")
    private Integer version;

    @ApiModelProperty(value = "角色集合")
    @JsonIgnore
    @ManyToMany(cascade = CascadeType.REFRESH, fetch = FetchType.LAZY)
    @JoinTable(name = "sys_user_role",
            joinColumns = {@JoinColumn(name = "user_id")},
            inverseJoinColumns = {@JoinColumn(name = "role_id")})
    @org.hibernate.annotations.ForeignKey(name = "none")
    private List<Role> roleList;

    @Transient
    private String certNoStr;

    @Transient
    private String sexStr;

    @Transient
    private String personnelTypeStr;

    @Transient
    private String certTypeStr;

    @Transient
    private String deptStr;

    @Transient
    private Long roleId;

    public String getCertNoStr() {
        return EncodeUtils.desDecode(this.certNo, EncodeUtils.BDCZX_3DES_KEY);
    }

    public String getSexStr() {
        Map<String, Map<String, String>> dictTypeMap = (Map<String, Map<String, String>>) RedissonUtils.getObject("dict");
        return dictTypeMap.get("sex").get(String.valueOf(this.sex));
    }

    public String getPersonnelTypeStr() {
        return this.personnelType.getTypeName();
    }

    public String getCertTypeStr() {
        return this.certType.getTypeName();
    }

    public String getDeptStr() {
        return this.getDept().getDepName();
    }
}
